//create onDomReady Event
window.onDomReady = initReady;

var flowpedia_config = new FlowPedia();
var flowpedia_data = new FlowPediaData();

// Initialize event depending on browser
function initReady(fn)
{
   //W3C-compliant browser
   if(document.addEventListener)
   {
      document.addEventListener("DOMContentLoaded", fn, false);
   }
   //IE
   else
   {
      document.onreadystatechange = function(){readyState(fn)}
   }
}
 
//IE execute function
function readyState(func)
{
   // DOM is ready
   if(document.readyState == "interactive" || document.readyState == "complete")
   {
      func();
   }
}
      
//execute as soon as DOM is loaded
window.onDomReady(onReady);
 
//do when DOM is ready
function onReady()
{
	var myDiv = document.createElement("div");
	var request;
	var i, j;
	
	// Microblogging Heading
	var h2 = document.createElement('h2');
	h2.innerHTML = 'Microblogging';
	document.getElementById('bodyContent').appendChild(h2);
	
	var div = document.createElement('div');
	div.setAttribute('id', 'flowpedia_microblogging');
	div.innerHTML = '<img alt="loading" src="' + flowpedia_config.mediawikiURI + 'extensions/FlowPedia/'+ flowpedia_config.loading + '" />';
	document.getElementById('bodyContent').appendChild(div);
	
	var skript = flowpedia_config.statusnetURI+"api/search.json"
	
	var searchString = wgTitle;
	var searchKeyWordsString = wgTitle;
	
	if(document.getElementById("catlinks") != null)
	{
	   var cats = document.getElementById("catlinks").getElementsByTagName("a");
	   if(cats.length > 0)
	   {
         for(i = 0; i < cats.length; i++)
         {
            if(cats[i].innerHTML != wgTitle && cats[i].innerHTML != flowpedia_config.categoryName)
            {
               searchString += ' ' + cats[i].innerHTML;
               searchKeyWordsString += ', ' + cats[i].innerHTML; 
            }
         }
      }
	}
	
	//searchKeyWords enthält immer mindestens den Titel
	
   request = new FlowPediaAjax(skript, true);
   request.addVar('q', searchString);
   request.setFunktion(function(){
         var obj = request.res();
         var cont = '', list = '';
         
         // set general heading
         flowpedia_data.setOutGeneral('<i>Gesuchte Stichworte: ' + searchKeyWordsString + '</i> - <a class="external text" href="' + flowpedia_config.statusnetURI + 'search/notice?action=noticesearch&q=' + encodeURIComponent(searchString) + '">Suche in statusNet öffnen</a> - <a class="flowpedia_cursor" onclick="flowpedia_config.applyView(\'list\');">Listenansicht</a> | <a class="flowpedia_cursor" onclick="flowpedia_config.applyView(\'cells\');">Zellenansicht</a><br/><br/><div>');
         
         
         if(obj.results.length > 0)
         {
            for(j = 0; j < obj.results.length; j++)
            {
               cont += '<div class="flowpedia_container">' + obj.results[j].text + '<br/><strong>' + obj.results[j].from_user + '</strong>, <a class="external text" href="' + flowpedia_config.statusnetURI + 'notice/' + obj.results[j].id + '">' + flowpedia_config.transformDate(obj.results[j].created_at) + '</a></div>';
               
               list += '<li>' + obj.results[j].text + '<br/>[' + obj.results[j].from_user + '], <a class="external text" href="' + flowpedia_config.statusnetURI + 'notice/' + obj.results[j].id + '">' + flowpedia_config.transformDate(obj.results[j].created_at) + '</a></li>';
            }
            flowpedia_data.setOutList('<ul>' + list + '</ul>');
            flowpedia_data.setOutCells(cont);
         }
         else
         {
            flowpedia_data.setOutList('<ul><li><i>keine Inhalte gefunden</i></li></ul>');
            flowpedia_data.setOutCells('<ul><li><i>keine Inhalte gefunden</i></li></ul>');
         }
         document.getElementById('flowpedia_microblogging').innerHTML = flowpedia_data.getOutCells();
   });
   request.senden();
}
